package com.smartcampusbackend.service.impl;

import com.smartcampusbackend.mapper.ActivitySignupMapper;
import com.smartcampusbackend.model.ActivitySignup;
import com.smartcampusbackend.model.ActivitySignupDetail;
import com.smartcampusbackend.model.Announcement;
import com.smartcampusbackend.service.ActivitySignupService;
import com.smartcampusbackend.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivitySignupServiceImpl implements ActivitySignupService {
    @Autowired
    private ActivitySignupMapper activitySignupMapper;
    
    @Autowired
    private AnnouncementService announcementService;

    @Override
    public int signup(ActivitySignup signup) {
        // 检查是否可以报名
        if (!canSignup(signup.getActivityId())) {
            throw new RuntimeException("活动报名已截止或已满员");
        }
        
        int result = activitySignupMapper.insertSignup(signup);
        return result;
    }

    @Override
    public List<ActivitySignup> getSignupsByActivityId(Integer activityId) {
        return activitySignupMapper.selectSignupsByActivityId(activityId);
    }

    @Override
    public ActivitySignup getSignupById(Integer id) {
        return activitySignupMapper.selectSignupById(id);
    }

    @Override
    public int approveSignup(Integer id, String status) {
        // 先获取报名信息
        ActivitySignup signup = activitySignupMapper.selectSignupById(id);
        if (signup == null) {
            throw new RuntimeException("报名记录不存在");
        }
        
        // 如果要审批通过，检查是否还能通过
        if ("APPROVED".equals(status) && !canApprove(signup.getActivityId())) {
            throw new RuntimeException("活动已满员，无法再审批通过");
        }
        
        // 直接通过报名ID更新状态
        int result = activitySignupMapper.updateSignupStatus(id, status);
        
        // 如果审批通过，检查并更新活动状态
        if (result > 0 && "APPROVED".equals(status)) {
            announcementService.checkAndUpdateActivityStatus(signup.getActivityId());
        }
        
        return result;
    }

    @Override
    public List<ActivitySignup> getSignupsByPublisher(String publisher) {
        return activitySignupMapper.selectSignupsByPublisher(publisher);
    }

    @Override
    public int getApprovedSignupCount(Integer activityId) {
        return activitySignupMapper.countApprovedSignups(activityId);
    }

    @Override
    public boolean canSignup(Integer activityId) {
        Announcement activity = announcementService.getAnnouncementById(activityId);
        if (activity == null || !"ACTIVITY".equals(activity.getType())) {
            return false;
        }
        
        // 检查活动状态
        if (!"SIGNUP_OPEN".equals(activity.getStatus())) {
            return false;
        }
        
        // 检查是否已截止
        if (activity.getSignupDeadline() != null && new Date().after(activity.getSignupDeadline())) {
            return false;
        }
        
        // 检查是否已满员
        if (activity.getActivityLimit() != null) {
            int approvedCount = getApprovedSignupCount(activityId);
            if (approvedCount >= activity.getActivityLimit()) {
                return false;
            }
        }
        
        return true;
    }

    @Override
    public boolean canApprove(Integer activityId) {
        Announcement activity = announcementService.getAnnouncementById(activityId);
        if (activity == null || !"ACTIVITY".equals(activity.getType())) {
            return false;
        }
        
        // 检查是否已满员
        if (activity.getActivityLimit() != null) {
            int approvedCount = getApprovedSignupCount(activityId);
            if (approvedCount >= activity.getActivityLimit()) {
                return false;
            }
        }
        
        return true;
    }
    
    @Override
    public int deleteSignupsByActivityId(Integer activityId) {
        return activitySignupMapper.deleteSignupsByActivityId(activityId);
    }
    
    @Override
    public List<ActivitySignup> getMySignups(String studentId) {
        return activitySignupMapper.selectSignupsByStudentId(studentId);
    }
    
    @Override
    public List<ActivitySignupDetail> getMySignupDetails(String studentId) {
        return activitySignupMapper.selectSignupDetailsByStudentId(studentId);
    }
    
    @Override
    public List<ActivitySignup> getSignupsByUserId(Integer userId) {
        return activitySignupMapper.selectSignupsByUserId(userId);
    }
    
    @Override
    public List<ActivitySignupDetail> getSignupDetailsByUserId(Integer userId) {
        return activitySignupMapper.selectSignupDetailsByUserId(userId);
    }
    
    @Override
    public List<ActivitySignup> getPendingApprovals(Integer activityId) {
        return activitySignupMapper.selectPendingApprovals(activityId);
    }
    
    @Override
    public List<ActivitySignup> getApprovalHistory(Integer activityId) {
        return activitySignupMapper.selectApprovalHistory(activityId);
    }
    
    @Override
    public List<ActivitySignup> getApprovalList(Integer activityId, String status) {
        List<ActivitySignup> result = activitySignupMapper.selectApprovalsByStatus(activityId, status);
        return result;
    }
    
    @Override
    public boolean canManageActivity(Integer activityId, String publisher) {
        Announcement activity = announcementService.getAnnouncementById(activityId);
        if (activity == null || !"ACTIVITY".equals(activity.getType())) {
            return false;
        }
        // 只有活动的发布者可以管理该活动
        return activity.getPublisher().equals(publisher);
    }
} 